<?php

session_start();
include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';
include 'inc/form_op.php';

$page_title = "Answer";
$debug = 0;

$user_id = $_SESSION['user_id'];

$new_question_fields = array("user_id", "title", "content", "tags");
function gen_ask_form() {
  $form_rows = array(
    "标题"=>"<input type='text' id='title' name='title' size=50 required/>",
	"内容"=>"<textarea id='content' name='content' required rows=6 cols=45></textarea>",
	"标签"=>"<input type='text' id='tags' name='tags' placeholder='请输入逗号分隔的词组' size=50 required/>",
	""=>"<button type='submit' id='new_question_submit' name='new_question_submit'>提交</button>"
  );
  $table_html = "<form name='new_question' method='post'>".
                "<input type='hidden' name='user_id' value=".$GLOBALS['user_id']." />".
				gen_twincol_table("new_question", $form_rows).
				"</form>";
  return $table_html;
}

function gen_answer_form($div_id, $question_id, $hint = "") {
	$html_str .= "<div id='$div_id' style='display:none'>$hint";
	$html_str .= "<form method='post'>";
	$html_str .= "<input type='hidden' name='question_id' value=$question_id/>";
	$html_str .= "<input type='hidden' name='user_id' value=".$GLOBALS['user_id']."/>";
	$html_str .= "<textarea name='content' rows=6 cols=50 required></textarea><br>";
	$html_str .= "标签：<input type='text' name='tags' size=48 placeholder='请输入以逗号分隔的词组，可以为空'><br/>";
	$html_str .= "<button type=submit name='new_answer_submit'>提交</button>&nbsp &nbsp<a href=javascript:hide_div('$div_id')>取消</a>";
	$html_str .= "</form></div>";
	return $html_str;
}

$new_answer_fields = array("question_id", "user_id", "content", "tags");
function get_open_questions() {
  $html_str = "";
  $sql = "SELECT question.id id, name, date_format(ask_time, '%m-%d %H:%i') ask_time, title, content ".
         "FROM question JOIN user_info ON question.user_id=user_info.id ".
         "WHERE question.id NOT ".
		 "IN ( ".
		 "SELECT DISTINCT question_id ".
		 "FROM answer) order by question.ask_time desc";
  $questions = db_get($sql);
  $list_items = array();
  if(0 == count($questions)) {
    $html_str .= "<p>暂时没有未回答的问题。</p>";
  }
  foreach($questions as $question) {
    $answer_div_id = "q_".$question['id'];
    $disp = $question['name'].'于'.$question['ask_time'].'问道：'.$question['title']."？&nbsp &nbsp <a href=javascript:show_div('$answer_div_id')>查看内容并回答</a>";
	$hint = '<p><i>'.str_replace(chr(13), '<br/>', $question['content']).'</i></p>';
	$disp .= gen_answer_form($answer_div_id, $question['id'], $hint);
	array_push($list_items, $disp);
  }
  $html_str .= gen_list($list_items);
  return $html_str;
}

function gen_search_form() {
  $html_str = "";
  return $html_str;
}

function get_left_content() {
  $html_str = "<h2>提问</h2>".gen_ask_form();
  $html_str .= "<h2>待回答问题</h2>".get_open_questions();
  return $html_str;
}

function get_answers_list($question_id) {
  $sql = "select name, date_format(answer_time, '%m-%d %H:%i') answer_time, content ".
         "from answer join user_info on answer.user_id = user_info.id ".
		 "where question_id = $question_id order by answer.answer_time desc";
  $answers = db_get($sql);
  $list_items = array();
  foreach($answers as $answer) {
    $item = $answer['name'].'于'.$answer['answer_time'].'答道：<br/>'.str_replace(chr(13), '<br/>', $answer['content']);
	array_push($list_items, $item);
  }
  $html_str = gen_list($list_items, 'ul');
  return $html_str;
}

function get_right_content() {
  //$html_str = "<h2>订阅的问答</h2>";
  //$html_str .= "<p>当前订阅：<a>修改</a></p>";
  $html_str = "<h2>问答</h2>";
  $sql = "SELECT question.id id, name, date_format(ask_time, '%m-%d %H:%i') ask_time, title, content ".
         "FROM question JOIN user_info ON question.user_id=user_info.id ".
         "WHERE question.id ".
		 "IN ( ".
		 "SELECT DISTINCT question_id ".
		 "FROM answer) order by question.ask_time desc";
  $list_items = array();
  $questions = db_get($sql);
  foreach($questions as $question) {
	$add_answer_div_id = "n_".$question['id'];
    $disp = $question['name'].'于'.$question['ask_time'].'问道：'.$question['title']."？&nbsp &nbsp<a href=javascript:show_div('$add_answer_div_id')>补充回答</a>";
	$disp .= "<p><i>".str_replace(chr(13), '<br/>', $question['content'])."</i></p>";
	$disp .= gen_answer_form($add_answer_div_id, $question['id']);
	$disp .= "<div>".get_answers_list($question['id'])."</div><br/>";
	array_push($list_items, $disp);
  }
  $html_str .= gen_list($list_items);
  //$html_str .= "<h2>全文检索</h2>".gen_search_form();
  return $html_str;
}

perform_insert_post("new_question_submit", "question", $new_question_fields);
perform_insert_post("new_answer_submit", "answer", $new_answer_fields);
$left_content = get_left_content();
$right_content = get_right_content();

echo gen_twincol_page($page_title, $left_content, $right_content, $debug);

?>